package com.bawei.module_home.ui.merchant

import android.app.DatePickerDialog
import android.app.Dialog

import android.os.Bundle
import android.widget.DatePicker
import androidx.fragment.app.DialogFragment
import java.util.Calendar


class TimePickerFragment : DialogFragment(), DatePickerDialog.OnDateSetListener {
    // 回调接口，用于传递选择的日期
    interface OnDateSelectedListener {
        fun onDateSelected(year: Int, month: Int, day: Int)
    }

    private var listener: OnDateSelectedListener? = null

    // 设置回调监听
    fun setOnDateSelectedListener(listener: OnDateSelectedListener) {
        this.listener = listener
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        // 获取当前日期作为默认值
        val calendar = Calendar.getInstance()
        val year = calendar.get(Calendar.YEAR)
        val month = calendar.get(Calendar.MONTH)
        val day = calendar.get(Calendar.DAY_OF_MONTH)

        // 创建 DatePickerDialog 实例
        return DatePickerDialog(
            requireContext(),
            this,  // 日期选择回调
            year,
            month,
            day
        )
    }

    // 日期选择完成后的回调
    override fun onDateSet(view: DatePicker, year: Int, month: Int, day: Int) {
        // 月份从 0 开始，所以 +1
        listener?.onDateSelected(year, month + 1, day)
    }

}